package bch.rocketmq.config;

import org.redisson.api.RRateLimiter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;

@Component
public class MySmartLifeCycle implements SmartLifecycle {
    private static final Logger log = LoggerFactory.getLogger(MySmartLifeCycle.class);

    private boolean isRunning = false;

    @Autowired
    private RRateLimiter rateLimiter;

    @Override
    public void start() {
        log.info("MySmartLifeCycle start");
        isRunning = true;
    }

    @Override
    public void stop() {
        log.info("MySmartLifeCycle stop");
        isRunning = false;
        rateLimiter.delete();
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public int getPhase() {
        return Integer.MAX_VALUE;
    }

    @Override
    public void stop(Runnable callback) {
        log.info("MySmartLifeCycle stop(Runnable callback)");
        SmartLifecycle.super.stop(callback);
    }

    @Override
    public boolean isAutoStartup() {
        return SmartLifecycle.super.isAutoStartup();
    }
}
